#ifndef _COMMON_H
#define _COMMON_H
//#include <stdint.h>
#include "platform.h"

#define SetBit8(x, y) do{(((x)) |= ((uint8_t)(1 << (y))));}while(0)
#define ClearBit8(x, y) do{(((x)) &= ~((uint8_t)(1 << (y))));}while(0)

#define SetBit16(x, y) do{(((x)) |= ((uint16_t)(1 << (y))));}while(0)
#define ClearBit16(x, y) do{(((x)) &= ~((uint16_t)(1 << (y))));}while(0)

#define SetBit32(x, y) do{(((x)) |= ((uint32_t)(1 << (y))));}while(0)
#define ClearBit32(x, y) do{(((x)) &= ~((uint32_t)(1 << (y))));}while(0)


#define U32SetBit(x,y) do{(((x)) |= ((uint32_t)(1 << (y))));}while(0)
#define U32ClearBit(x,y) do{(((x)) &= ~((uint32_t)(1 << (y))));}while(0)

#define SetBit64(x, y) do{(((x)) |= ((uint64_t)(1 << (y))));}while(0)
#define ClearBit64(x, y) do{(((x)) &= ~((uint64_t)(1 << (y))));}while(0)

#define ToggleBit8(x, y) do{(((x)) ^= ((uint8_t)(1 << (y))));}while(0)

#define U16GetHighByte(x) ((uint8_t)((((uint16_t)(x)) >> 8) & 0xFF))
#define U16GetLowByte(x) ((uint8_t)((((uint16_t)(x)) >> 0) & 0xFF))

#define U16SetHighByte(x, y) do { \
    (x) = (uint16_t)(( (x) & 0x00FF ) | ( ( (uint16_t)(y) & 0xFF ) << 8 )); \
} while(0)

#define U16SetLowByte(x, y) do { \
    (x) = (uint16_t)(( (x) & 0xFF00 ) | ( ( (uint16_t)(y) & 0xFF ) << 0 )); \
} while(0)

#define U32GetHighWord(x) ((uint16_t)((((uint32_t)(x)) >> 16) & 0xFFFF))
#define U32GetLowWord(x) ((uint16_t)((((uint32_t)(x)) >> 0) & 0xFFFF))

#define U8GetBitValue(val,x) ((uint8_t)((((uint8_t)(val)) >> (x)) & 0x01))

#define BITS(start, end)             ((0xFFFFFFFFUL << (start)) & (0xFFFFFFFFUL >> (31U - (uint32_t)(end))))

#define GET_BITS(regval, start, end) (((regval) & BITS((start),(end))) >> (start))

#define U16SetBitField(Ram, Start, End, Val) do { \
    uint16_t _start = (Start) > 15 ? 15 : (Start); \
    uint16_t _end = (End) > 15 ? 15 : (End); \
    uint16_t _mask_val = (_end >= _start) ? (_end - _start + 1) : 0; \
    uint16_t _val_mask = (_mask_val == 16) ? 0xFFFF : ((1U << _mask_val) - 1); \
    uint16_t _field_mask = _val_mask << _start; \
    (Ram) = ((Ram) & ~_field_mask) | (((Val) & _val_mask) << _start); \
} while (0)

#define U32SetBitField(Ram, Start, End, Val) do { \
    uint16_t _start = (Start) > 31 ? 31 : (Start); \
    uint16_t _end = (End) > 31 ? 31 : (End); \
    uint16_t _mask_val = (_end >= _start) ? (_end - _start + 1) : 0; \
    uint32_t _val_mask = (_mask_val == 32) ? 0xFFFFFFFF : ((1U << _mask_val) - 1); \
    uint32_t _field_mask = _val_mask << _start; \
    (Ram) = ((Ram) & ~_field_mask) | (((Val) & _val_mask) << _start); \
} while (0)

typedef struct{
    uint16_t AimValue;//目标值
    uint16_t CurrentValue;//当前值
    uint16_t Delta;//增量值
    uint16_t MaxDelta;//最大增量值
    uint16_t MinValue;//最小值
}LadderValue_t;


extern uint8_t data_swap_u8(uint8_t  data);
extern void memcpy_dma(void  * const dst, void const * const src, uint16_t n);
extern uint16_t LadderValueSet(LadderValue_t * pLadderValueInfo);
#endif
